﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;

namespace Thurn_und_Taxis.components
{

    public delegate void JoinRoomRequestHandler(object sender, Int32 roomId);
    public delegate void CreateRoomRequestHandler(object sender, String roomName);

    /// <summary>
    /// Interaction logic for ServerRoomsBox.xaml
    /// </summary>
    public partial class ServerRoomsBox : UserControl
    {
        public event JoinRoomRequestHandler JoinRoomRequest;
        public event CreateRoomRequestHandler CreateRoomRequest;
        private ArrayList _roomList;

        public ServerRoomsBox()
        {
            InitializeComponent();
        }

        public void UpdateRooms(ArrayList roomsList)
        {
            _roomList = roomsList;
            listBoxRooms.Items.Clear();

            for (int i = 0; i < roomsList.Count; i++)
            {
                RoomBox box = new RoomBox();
                TTData.Room r = (TTData.Room)roomsList[i];
                box.SetRoomName(r.Name);
                box.SetStarted(r.IsStarted);

                for (int j = 0; j < r.ListPlayer.Count; j++)
                {
                    if (r.ListPlayer[j] != null)
                    {
                        if (j == 0)
                            box.SetPlayer1(r.ListPlayer[j]);
                        else if (j == 1)
                            box.SetPlayer2(r.ListPlayer[j]);
                        else if (j == 2)
                            box.SetPlayer3(r.ListPlayer[j]);
                        else
                            box.SetPlayer4(r.ListPlayer[j]);
                    }
                }

                listBoxRooms.Items.Add(box);
            }
        }

        public void ShowErrorCode()
        {
            labelError.Visibility = Visibility.Visible;
        }

        public void UpdatePlayers(List<String> playersList)
        {
            listBoxPlayers.Items.Clear();

            for (int i = 0; i < playersList.Count; i++)
            {
                listBoxPlayers.Items.Add(playersList[i]);
            }
        }

        protected virtual void OnJoinRoomRequest(Int32 roomId)
        {
            if (JoinRoomRequest != null) JoinRoomRequest(this, roomId);
        }

        protected virtual void OnCreateRoomRequest(String roomName)
        {
            if (CreateRoomRequest != null) CreateRoomRequest(this, roomName);
        }

        private void btnCreateRoom_Click(object sender, RoutedEventArgs e)
        {
            String roomName = textBoxRoomName.Text;
            if (roomName != "")
            {
                OnCreateRoomRequest(roomName);
            }

        }

        private void listBoxRooms_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            int index = listBoxRooms.SelectedIndex;
            if (index != -1)
            {
                int roomId = ((TTData.Room)_roomList[index]).ID;
                OnJoinRoomRequest(roomId);
            }
        }
    }
}
